package com.tao.gulimall.coupon.service.impl;

import com.tao.common.to.MemberPrice;
import com.tao.common.to.SkuReductionTo;
import com.tao.gulimall.coupon.entity.MemberPriceEntity;
import com.tao.gulimall.coupon.entity.SkuLadderEntity;
import com.tao.gulimall.coupon.service.MemberPriceService;
import com.tao.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.Query;

import com.tao.gulimall.coupon.dao.SkuFullReductionDao;
import com.tao.gulimall.coupon.entity.SkuFullReductionEntity;
import com.tao.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

	@Autowired
	private SkuLadderService skuLadderService;


	@Autowired
	private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public void saveSkuReduction(SkuReductionTo skuReductionTo) {
		//6.4 保存sku的优惠, 满减等信息, 对应 gulimall_sms -> sms_sku_loadder\sms_sku_full_reduction\sms_member_price
		// sms_sku_loadder
		SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
		skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
		skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
		skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
//		skuLadderEntity.setPrice();
		skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
		if(skuReductionTo.getFullCount() >0){
			skuLadderService.save(skuLadderEntity);
		}

		// sms_sku_full_reduction
		SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
		BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);
		if(skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
			this.save(skuFullReductionEntity);
		}

		// sms_member_price
		List<MemberPrice> skuReductionToMemberPrice = skuReductionTo.getMemberPrice();
		List<MemberPriceEntity> memberPriceEntities = skuReductionToMemberPrice.stream().map(item -> {
			MemberPriceEntity memberPrice = new MemberPriceEntity();
			memberPrice.setSkuId(skuReductionTo.getSkuId());
			memberPrice.setAddOther(1);
			memberPrice.setMemberPrice(item.getPrice());
			memberPrice.setMemberLevelId(item.getId());
			memberPrice.setMemberLevelName(item.getName());
			return memberPrice;
		}).filter(item->{
			return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
		}).collect(Collectors.toList());
		memberPriceService.saveBatch(memberPriceEntities);

	}

}